
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
    模拟简单web服务，可以返回hello.html给浏览器
 */
public class MyTomcat {

    public static void main(String[] args) throws Exception {

        //1.在9998端口监听
        ServerSocket serverSocket = new ServerSocket(9998);

        //如果serverSocket没有关闭，就一直等待连接，不停地等待
        while(!serverSocket.isClosed()) {
            System.out.println("====我的web服务在9998端口监听====");
            //2.等待浏览器/客户端连接，得到socket
            //该socket用于通信
            Socket socket = serverSocket.accept();

            //3.通过socket 得到输出流
            OutputStream outputStream = socket.getOutputStream();
            //返回给浏览器/客户端
            BufferedReader bufferedReader = new BufferedReader(new FileReader("tomcat/src/hello.html"));
            String buf = "";
            //循环读取 tomcat/src/hello.html
            while((buf = bufferedReader.readLine()) != null) {
                outputStream.write(buf.getBytes());
            }
//            socket.shutdownOutput();
            outputStream.close();
            socket.close();
        }
        serverSocket.close();
    }
}
